package club.ruanx.security;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * 当服务挂掉之后短信提醒
 *
 * @author 阮胜
 * @date 2019/1/7 10:38
 */
@Component
@Slf4j
public class SmsNotifier extends AbstractStatusChangeNotifier {

    private static final String STATUS_CHANGED = "STATUS_CHANGED";
    private static final String OFFLINE = "OFFLINE";

    public SmsNotifier(InstanceRepository repositpry) {
        super(repositpry);
    }

    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            InstanceStatusChangedEvent statusChange = (InstanceStatusChangedEvent) event;
            if (statusChange.getType().equals(STATUS_CHANGED) && statusChange.getStatusInfo().getStatus().equals(OFFLINE)) {
                String serverName = instance.getRegistration().getName();
                log.error("{}服务挂掉了", serverName);
            }
        });
    }
}
